package com.zaofan.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.Dynamic;
import com.zaofan.bean.DynamicDTO;
import com.zaofan.bean.PageBean;
import com.zaofan.service.DynamicService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

@WebServlet("/admin/dynamic")
public class DynamicController extends BaseServlet {
	private static final long serialVersionUID = -5377620282223515871L;
	private DynamicService dynamicService = new DynamicService();

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			String url = "dynamic?action=list";
			Integer begin = 1;
			try {
				begin = Integer.valueOf(request.getParameter("begin"));
			} catch (Exception e) {
				begin = 1;
			}

			Integer end = 10;
			try {
				end = Integer.valueOf(request.getParameter("end"));
			} catch (Exception e) {
				end = 10;
			}
			String userMobile = request.getParameter("userMobile");
			String userName = request.getParameter("userName");
			PageBean pageBean = new PageBean();
			int page = begin - 1;
			pageBean.setBegin(page * 10 + 1);
			List<DynamicDTO> dynamics = dynamicService.findAll((page * 10) + "", end + "", userMobile, userName);
			Long count = dynamicService.count(userMobile, userName);
			pageBean.setEnd(page * 10 + dynamics.size());
			pageBean.setTotal(count.intValue());
			pageBean.setPageInfo(WebUtils.getPageData(count.intValue(), 10, page + 1, url));
			request.setAttribute("dynamics", dynamics);
			request.setAttribute("pageBean", pageBean);
			request.getRequestDispatcher("/admin/dynamic/dynamicList.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	protected void to_add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			request.setAttribute("action", "add");
			request.getRequestDispatcher("/admin/dynamic/dynamicAdd.jsp").forward(request, response);
		} catch (Exception e) {
		}
	}

	protected void to_edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Dynamic dynamic = dynamicService.findById(id);
			request.setAttribute("dynamic", dynamic);
			request.setAttribute("action", "update");
			request.getRequestDispatcher("/admin/dynamic/dynamicAdd.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Dynamic dynamic = dynamicService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", dynamic, response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("查询失败!!", response);
	}

	protected void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Dynamic dynamic = dynamicService.findById(id);
			dynamic.setIsDelete(dynamic.getIsDelete() == 0 ? 1 : 0);
			dynamicService.update(dynamic);
			JsonUtils.writeSuccessJson("删除成功!!", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("删除失败!!", response);
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Dynamic dynamic = WebUtils.write2bean(request, Dynamic.class);
			dynamicService.add(dynamic);
			JsonUtils.writeSuccessJson("添加成功!!", "", response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("属性错误!!", response);
		}
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Dynamic dynamic = WebUtils.write2bean(request, Dynamic.class);
			dynamicService.updateDynamic(dynamic);
			JsonUtils.writeSuccessJson("更新成功!!", "", response);
		} catch (Exception e) {
			JsonUtils.writeFailJson("更新失败!!", response);
		}
	}

	protected void type(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String temp = request.getParameter("id");
		try {
			Integer id = Integer.valueOf(temp);
			Dynamic dynamic = dynamicService.findById(id);
			dynamic.setType(dynamic.getType() == 1 ? 2 : 1);
			dynamicService.update(dynamic);
			JsonUtils.writeSuccessJson("更新成功!!", "", response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("参数错误!!", response);
		}
		JsonUtils.writeFailJson("更新失败!!", response);
	}

}
